import 'dart:async';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:jicaimao_flutter/utils/sp_key.dart';
import 'package:jicaimao_flutter/utils/sp_utils.dart';
import 'package:jicaimao_flutter/views/custom/loading_dialog.dart';
import 'package:jicaimao_flutter/views/mine/SecurityCentre.dart';

/// 转出页面
class CheckOut extends StatefulWidget {
  @override
  _CheckOutState createState() => _CheckOutState();
}

/// 使用with  SingleTickerProviderStateMixin展示一些动画效果
class _CheckOutState extends State<CheckOut>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  TextEditingController _checkInController = new TextEditingController();
  TextEditingController _passWordController = new TextEditingController();



  bool isBlifeHide = false;
  bool _bscAdress = false;
  var basadressStr;
  final List<Tab> myTabs = <Tab>[
    new Tab(text: '转出积分到B生活'),
    new Tab(text: '转出积分至BSC钱包'),
  ];

   @override
  void initState() {
    super.initState();
//    _showDialog();
    basadressStr = SpUtils.getString(SpKey.KEY_BOX_BSCADRESS);
    _bscAdress = basadressStr != null ? true : false;
    _tabController = new TabController(
        vsync: this, //动画效果的异步处理，默认格式，背下来即可
        length: 2 //需要控制的Tab页数量
        );
  }

  //当整个页面dispose时，记得把控制器也dispose掉，释放内存
  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('转出',style: TextStyle(color: Colors.black),),
        elevation: 0.0,
        backgroundColor: Colors.white,
        bottom: new TabBar(tabs: <Widget>[
          new Tab(text: '转出积分到B生活',),
          new Tab(text:'转出积分至BSC钱包',)
        ],
        labelColor: Colors.deepPurple,
        unselectedLabelColor: Colors.black,
        indicatorColor: Color(0xFFad5afe),
        controller: _tabController,),
        iconTheme: IconThemeData(
          color: Colors.black
        ),
      ),
      body:new TabBarView(
        controller: _tabController,
        children: <Widget>[
          _blifeLayout(context,1),
          _blifeLayout(context,2),

        ],
      )

    );
  }
  /// 转到B生活布局1 转到BSC  2
  Widget _blifeLayout(BuildContext context,int type){
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          Offstage(
            offstage: type == 1 ?!isBlifeHide :isBlifeHide,
            child: Column(
              children: <Widget>[
                Container(
                  height: 150,
                  alignment: Alignment.center,
                  child: Center(
                    child: Text(type == 1 ?'还没有绑定B生活':'还没有绑定BSC钱包',style: TextStyle(color: Colors.grey),),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    _goBindBlifeOrBSC();
                  },
                  child: Container(
                    height: 40.0,
                    width: double.infinity,
                    margin: EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 0.0),
                    alignment: Alignment.center,

                    decoration: BoxDecoration(
                        color: Color(0xFFad5afe),
                        border: Border.all(color: Color(0xFFad5afe), width: 1.0),
                        borderRadius: BorderRadius.circular(3.0),
                    ),

                    padding: EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 0.0),
                    child: new Text('去绑定',style: TextStyle(color: Colors.white,),)

                  ),
                )


              ],
            ),
          ),
          Offstage(
            offstage: type == 1 ?isBlifeHide :!isBlifeHide,
            child: Column(

              children: <Widget>[
                Container(
                  height: 40.0,
                  margin: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                  child: Row(
                    children: <Widget>[
                      Expanded(child:
                      new Text('转出积分(BSC)',style: new TextStyle(

                          fontSize: 18.0,
                          color: Color(0xFF333333))
                      ),flex: 1,),
                      new Text('')
                    ],
                  ),
                ),
            Container(
              color: Colors.white,
              padding:EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
              child:  TextField(
                textAlign: TextAlign.start,
                keyboardType: TextInputType.number,
                style: TextStyle(
                    color: Color(0xFF666666),
                    fontSize: 13.0
                ),
                decoration: InputDecoration(
                    hintText: "请输入要转出的积分数量",

                    hintStyle: TextStyle(
                        color: Color(0xFF999999),
                        fontSize: 14.0
                    ),
                    disabledBorder: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none
                ),
                controller: _checkInController,
                textInputAction: TextInputAction.next,
                maxLines: 1,
              ),
            ),
                Container(
                  height: 40.0,
                  margin: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                  child: Row(
                    children: <Widget>[
                      Expanded(child:
                      new Text('BSC钱包地址',style: new TextStyle(

                          fontSize: 18.0,
                          color: Color(0xFF333333))
                      ),flex: 1,),
                      new Text('')
                    ],
                  ),
                ),
                Offstage(
                  offstage: _bscAdress,
                  child: Text('宝箱地址'),
                ),
                Offstage(
                  offstage: !_bscAdress,
                  child: Text(basadressStr),
                ),
                new Container(
                  height: 40.0,
                  width: 400.0,
                  margin: EdgeInsets.all(20.0),
                  child:  RaisedButton(onPressed: (){
                     _blifeCheck(context,type);
                  },
                    color: Color(0xFFB969FF),
                    child: Text('转出' ,style: TextStyle(
                        fontSize: 16.0,
                        fontWeight: FontWeight.bold
                    ),),
                    textColor: Colors.white,


                  ),
                ),
               

              ],
            ),
          )
        ],
      ),
    );
  }

  /// 去绑定B生活 Or BSC
   void  _goBindBlifeOrBSC(){
       Navigator.push(context, MaterialPageRoute(builder: (context) => SecurityCentre()));
   }

   /// 转到B生活
   _blifeCheck(BuildContext context,int type){
    var score = _checkInController.text;
    if( score.isEmpty){
      Fluttertoast.showToast(msg: "请输入转出积分数量");
      return ;
    }
    if( null == basadressStr){
      Fluttertoast.showToast(msg: "BSC地址不能为空");
      return;
    }
/// 输入密码
    showDialog<Null>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context){
        return new Dialog(
          child: SizedBox(

            width: 100.0,
            height: 150.0,
            child: new Column(
              children: <Widget>[
                Padding(padding: EdgeInsets.only(top: 15.0)),
                Text('请输入密码',style: TextStyle(color: Color(0xFF333333),fontSize: 15.0),),
                TextField(
                  controller: _passWordController,
                  keyboardType: TextInputType.number,
                  maxLength: 6,
                  obscureText: false,

                 ),
                Divider(height: 1.0,color: Colors.grey,),
                Row(
                  children: <Widget>[
                    Expanded(child: Container(
                      margin: EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
                      child: RaisedButton(onPressed: (){
                        Navigator.pop(context);
                      },
                        color: Colors.grey,child: new Center(child: Text('取消'),),
                      ),
                    ),
                      flex: 1,
                    ),
                    Expanded(child: Container(
                      margin: EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
                      child: RaisedButton(onPressed: (){},
                        color: Colors.indigo,child: new Center(child: Text('确认',
                        style: TextStyle(color: Colors.white),),),
                      ),
                    ),flex: 1,)
                  ],
                )
              ],
            ),

          ),
        );
      }
    );



//    _buildCenterButton(context);

   }

   // 展示dilog
   _showDialog(){
     showDialog<Null>(
         context: context, //BuildContext对象
         barrierDismissible: false,
         builder: (BuildContext context) {
           return new LoadingDialog( //调用对话框
             text: '正在转出...',
           );
         });
   }
   /// 关闭loading
  _hideDialog(){
    Navigator.pop(context);
  }
 // 输入密码
  Widget _buildCenterButton(BuildContext context) {

    return new Container(
        alignment: Alignment.center,
        child: new Container(
          color: Colors.black,
          height: 100.0,
          child:Text('asdsadsfd'),
        ));
  }

  Widget _buildButton(BuildContext context){
    return Column(
      children: <Widget>[
        Center(child: Text('请输入支付密码'),),
        Container(
          color: Colors.white,
          padding:EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
          child:  TextField(
            textAlign: TextAlign.start,
            keyboardType: TextInputType.number,
            style: TextStyle(
                color: Color(0xFF666666),
                fontSize: 13.0
            ),
            decoration: InputDecoration(
                hintText: "请输入密码",

                hintStyle: TextStyle(
                    color: Color(0xFF999999),
                    fontSize: 14.0
                ),
                disabledBorder: InputBorder.none,
                enabledBorder: InputBorder.none,
                focusedBorder: InputBorder.none
            ),
            controller: _passWordController,
            textInputAction: TextInputAction.next,
            maxLines: 1,
          ),
        ),
      ],
    );
  }

}
